﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PagedList;
using BLL;
using DLL;

namespace QuanLyBanLe
{
    public partial class FormOutProductHistory : Form
    {
        private OutProductService m_OutProductService;
        private int m_PageNumber;
        private int m_PageSize;
        private int m_MaxPage;
        private bool m_EnabledEvent;
        private int m_TotalRecord;
        private bool isFiltered = false;
        private IList<pOutProdPaper_Get_Result> m_OutProductHistory;

        public FormOutProductHistory()
        {
            m_OutProductService = new OutProductService();
            m_PageNumber = 1;
            m_PageSize = 20;
            m_EnabledEvent = false;
            InitializeComponent();
            dtgOutProdPaper.AutoGenerateColumns = false;
        }

        private void btnFirst_Click(object sender, EventArgs e)
        {
            m_PageNumber = 1;
            ChangePageInProdPaper();
        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            m_PageNumber = m_PageNumber > 1 ? m_PageNumber - 1 : m_PageNumber;
            ChangePageInProdPaper();
        }

        private void mnrPageIndex_ValueChanged(object sender, EventArgs e)
        {
            if (m_EnabledEvent)
            {
                m_PageNumber = Convert.ToInt32(mnrPageIndex.Value);
                ChangePageInProdPaper();
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            m_PageNumber = m_PageNumber < m_MaxPage ? m_PageNumber + 1 : m_PageNumber;
            ChangePageInProdPaper();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            m_PageNumber = m_MaxPage;
            ChangePageInProdPaper();
        }

        private void SetEnabledForPagingNavigator()
        {
            if (m_PageNumber == m_MaxPage)
            {
                if (m_PageNumber == 1)
                {
                    btnFirst.Enabled = false;
                    btnPrev.Enabled = false;
                    btnLast.Enabled = false;
                    btnNext.Enabled = false;
                }
                else
                {
                    btnFirst.Enabled = true;
                    btnPrev.Enabled = true;
                    btnLast.Enabled = false;
                    btnNext.Enabled = false;
                }
            }
            else if (m_PageNumber == 1)
            {
                btnFirst.Enabled = false;
                btnPrev.Enabled = false;
                btnLast.Enabled = true;
                btnNext.Enabled = true;
            }
            else
            {
                btnFirst.Enabled = true;
                btnPrev.Enabled = true;
                btnLast.Enabled = true;
                btnNext.Enabled = true;
            }
        }

        private void FormOutProductHistory_Load(object sender, EventArgs e)
        {
            m_OutProductHistory = m_OutProductService.GetOutProdPaper(DateTime.Now, DateTime.Now);
            BindPaperInfoToGrid();
            this.Text += FormMain.GetShopInfo();
        }

        private void ChangePageInProdPaper()
        {
            BindPaperInfoToGrid();
        }

        private void BindPaperInfoToGrid()
        {
            m_EnabledEvent = false;
            var outProductDetails = m_OutProductHistory.ToPagedList(m_PageNumber, m_PageSize);
            dtgOutProdPaper.DataSource = outProductDetails.ToList();
            m_TotalRecord = outProductDetails.TotalItemCount;
            m_MaxPage = outProductDetails.PageCount;
            if (m_TotalRecord > 0)
            {
                lblTotalPage.Text = "/" + m_MaxPage.ToString();
                mnrPageIndex.Minimum = 1;
                mnrPageIndex.Enabled = true;
                mnrPageIndex.Maximum = m_MaxPage;
                mnrPageIndex.Value = m_PageNumber;
                SetEnabledForPagingNavigator();
            }
            else
            {
                m_PageNumber = 0;
                mnrPageIndex.Minimum = 0;
                mnrPageIndex.Value = 0;
                mnrPageIndex.Enabled = false;
                btnFirst.Enabled = false;
                btnPrev.Enabled = false;
                btnLast.Enabled = false;
                btnNext.Enabled = false;
                lblTotalPage.Text = string.Empty;
            }

            for (int i = 0; i < dtgOutProdPaper.Rows.Count; i++)
            {
                dtgOutProdPaper["colRowNumber", i].Value = (m_PageNumber - 1) * m_PageSize + i + 1;
            }
            m_EnabledEvent = true;
        }

        private void btnFilter_Click(object sender, EventArgs e)
        {
            m_EnabledEvent = false;
            isFiltered = true;
            m_PageNumber = 1;
            m_OutProductHistory = m_OutProductService.GetOutProdPaper(dtpFromDate.Value, dtpToDate.Value);
            BindPaperInfoToGrid();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dtgOutProdPaper_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == dtgOutProdPaper.Columns["colViewDetail"].Index && e.RowIndex != -1)
            {
                var paperId = Convert.ToInt32(dtgOutProdPaper["colId", e.RowIndex].Value.ToString());
                var paper = m_OutProductHistory.Where(x => x.Id == paperId).FirstOrDefault();
                FormOutProdPaper form = new FormOutProdPaper(paperId, paper.Date, paper.ToShopId);
                form.ShowDialog();
            }
            else if (e.ColumnIndex == dtgOutProdPaper.Columns["colUpdate"].Index && e.RowIndex != -1)
            {
                var paperId = Convert.ToInt32(dtgOutProdPaper["colId", e.RowIndex].Value.ToString());
                var paper = m_OutProductHistory.Where(x => x.Id == paperId).FirstOrDefault();
                FormOutProdPaper form = new FormOutProdPaper(paperId, paper.Date, true , paper.ToShopId);
                form.ShowDialog();
                ReloadData();
            }
        }

        public void ReloadData()
        {
            if (isFiltered)
            {
                m_OutProductHistory = m_OutProductService.GetOutProdPaper(dtpFromDate.Value, dtpToDate.Value);
            }
            else
            {
                m_OutProductHistory = m_OutProductService.GetOutProdPaper(DateTime.Now, DateTime.Now);
            }
            ChangePageInProdPaper();
        }

    }
}
